﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using System.Data;

namespace Simple.Services;

public class QHSEGeneralDangerService
{
    private readonly SimpleDbContext _context;
    public QHSEGeneralDangerService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEGeneralDangerModel>> GetAsync()
    {
        var generalDangers = await _context.Set<QHSESysGeneralDanger>().ToListAsync();
        var models = MapperHelper.Map<List<QHSEGeneralDangerModel>>(generalDangers);
        return models;
    }
    public async Task<PageResultModel<QHSEGeneralDangerModel>> QueryByViolationTypeCodeAsync(GeneralDangerInputModel input)
    {
        var result = new PageResultModel<QHSEGeneralDangerModel>();
        var query = _context.Set<QHSESysGeneralDanger>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(input.DangerTypeCode))
        {
            query = query.Where(u => EF.Functions.Like(u.DangerTypeCode, $"%{input.DangerTypeCode}%"));

        }
        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        input.PageSize = result.TotalRows;
        query = query.Page(input.PageNo, input.PageSize);
        var generalDangers = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEGeneralDangerModel>>(generalDangers);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

}
